package com.bjpowernode.crm.setting.service.impl;

import com.bjpowernode.crm.setting.dao.UserDao;
import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.setting.exception.LoginException;
import com.bjpowernode.crm.setting.service.UserService;
import com.bjpowernode.crm.util.DateTimeUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务层，处理业务
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    @Transactional
    public User login (User user) throws LoginException {
        //获取当前浏览器ip地址
        String loginIp=user.getAllowIps();
        //调用dao查询用户信息
        User loginUser=userDao.login(user);
        //获取当前用户状态
        if (loginUser==null){
            throw new LoginException("账号或密码错误,请重新登录");
        }else {
            String state=loginUser.getLockState();
            //获取用户的账号过期时间
            String expireTime=loginUser.getExpireTime();
            if (!loginUser.getAllowIps().contains(loginIp)){
                throw new LoginException("当前ip地址不正确，请检查");
            }else if ("0".equals(state)){
                throw new LoginException("当前账户已锁定,请重新激活");
            }else if(DateTimeUtil.getSysTime().compareTo(expireTime)>0){
                throw new LoginException("当前账号已超时,请重新激活");
            }

        }
        return loginUser;
    }

    @Override
    @Transactional
    public List<User> selectUsers() {
        //调用dao查询所有用户信息
        List<User> userList=userDao.selectUsers();
        return userList;
    }
}
